package com.htd.htdapmservice.mybatis;

import com.htd.htdapmcommon.mybatis.DalBaseMyBatis;
import com.htd.htdapmdmo.OneAlertAlarmsRecordInfo;
import com.htd.htdapmservice.dao.OneAlertAlarmsRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Repository
public class OneAlertAlarmsRecordMapper extends DalBaseMyBatis<OneAlertAlarmsRecordInfo> implements OneAlertAlarmsRecordDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

	/**
	 * 保存调用查询告警时输出的参数到数据库
	 * 
	 */
	@Override
	public void saveSearchAlarmInto(OneAlertAlarmsRecordInfo oneAlertAlarmsRecordInfo) {
		this.sqlSessionTemplate.insert(this.getEntityName() + ".insertSelective", oneAlertAlarmsRecordInfo);
	}

	/**
	 * 批量保存调用查询告警时输出的参数到数据库
	 * 
	 */
	@Override
	public void batchSave(List<OneAlertAlarmsRecordInfo> alarmsRecordInfoList) {
		this.sqlSessionTemplate.insert(this.getEntityName() + ".batchSave", alarmsRecordInfoList);
		
	}

	/**
	 * 从表"one_alert_alarms_record"查询出所有"alarm_id"
	 * 
	 */
	@Override
	public List<String> getAllAlarmId() {
		return this.sqlSessionTemplate.selectList(this.getEntityName() + ".getAllAlarmId");
	}

	
	/**
	 * 批量保存调用查询告警时输出的参数到数据库
	 * 
	 */
	@Override
	public void batchUpdate(final List<OneAlertAlarmsRecordInfo> updateAlarmsList) {
        final String sql = "UPDATE ONE_ALERT_ALARMS_RECORD SET STATUS = ?,UPDATE_TIME = NOW() WHERE ALARM_ID=?";
        this.jdbcTemplate.batchUpdate(sql,new BatchPreparedStatementSetter() {
            @Override
            public int getBatchSize() {
                return updateAlarmsList.size();
            }
            @Override
            public void setValues(PreparedStatement ps, int i)throws SQLException {
                ps.setString(1,updateAlarmsList.get(i).getStatus());
                ps.setString(2,updateAlarmsList.get(i).getAlarmId());
            }
        });
	}

	@Override
	public List<OneAlertAlarmsRecordInfo> queryData() {
		return this.sqlSessionTemplate.selectList(this.getEntityName() + ".queryData");
	}

	/**
	 * "one_alert_alarms_record"中查询出所有status !="ACTIVE"的数据
	 * 
	 */
	@Override
	public List<OneAlertAlarmsRecordInfo> getListByStatus(String status) {
		return this.sqlSessionTemplate.selectList(this.getEntityName() + ".getListByStatus",status);
	}

}